<template>
  <div ref="chartInst" style="width: 100%; height: 350px"></div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'PieChartIndex',
  props: {
    chartData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      chart: null,
      option: {
        color: ['#3aa0ff', '#36cbcb', '#4dcb73', '#fad337', '#f2637b', '#975fe4'],
        tooltip: {
          trigger: 'item'
        },
        legend: {
          top: '5%',
          left: 'center'
        },
        series: [
          {
            type: 'pie',
            radius: ['40%', '70%'],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: '#fff',
              borderWidth: 2
            },
            label: {
              show: false,
              position: 'center'
            },
            emphasis: {
              label: {
                show: true,
                fontSize: 20
              }
            },
            labelLine: {
              show: false
            },
            data: []
          }
        ]
      }
    }
  },
  watch: {
    chartData: {
      deep: true,
      immediate: true,
      handler: function () {
        this.init()
      }
    }
  },
  methods: {
    init() {
      try {
        this.option.series[0].data = this.chartData
        this.chart = echarts.init(this.$refs.chartInst)
        this.chart.setOption(this.option)
      } catch (error) {}
    }
  }
}
</script>
